package com.yoyo.itravel.dao;

import com.yoyo.itravel.bean.Flight;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;

/**
 * 航班表相关操作接口【持久层】
 * @Author: liuchaojie
 * @Date 2020/6/17 9:36
 */
@Repository
public interface IFlightDao {
    /**
     * 增加航班
     * @return
     * @throws SQLException
     */
    @Insert("insert into tb_flight(flightId,airlineId,planTypeId,flightNum,departCity,arrivalCity,departAirport,arrivalAirport,departTime,arrivalTime,isOnsell)" +
            " values(#{flight.flightId},#{flight.airlineId},#{flight.planTypeId},#{flight.flightNum},#{flight.departCity},#{flight.arrivalCity},#{flight.departAirport},#{flight.arrivalAirport},#{flight.departTime},#{flight.arrivalTime},#{flight.isOnsell})")
    boolean insertFlight(@Param("flight") Flight flight) throws SQLException;

    /**
     * 根据flightId删除一条航班信息
     * @param flightId
     * @return
     * @throws SQLException
     */
    @Delete("delete from tb_flight where flightId=#{flight}")
    boolean deleteByFlightId(String  flightId) throws SQLException;

    /**
     * 查询所有的航班
     * @return
     * @throws SQLException
     */
    @Select("select * from tb_flight")
    List<Flight> selectAll() throws SQLException;

    /**
     * 根据 airlineId、departCity、arrivalCity、departTime查询航班
     * @return
     * @throws SQLException
     */
    @Select("select * from tb_flight where airlineId=#{flight.airlineId} and departCity=#{flight.departCity} and arrivalCity=#{flight.arrivalCity} and departTime <= #{flight.departTime}")
    List<Flight> selectSomeByCondition(@Param("flight") Flight flight) throws SQLException;

    /**
     * 修改航班信息
     * @param flight
     * @return
     * @throws SQLException
     */
    @Update("update tb_flight set airlineId=#{flight.airlineId},planTypeId=#{flight.planTypeId},flightNum=#{flight.flightNum},departCity=#{flight.departCity},arrivalCity=#{flight.arrivalCity},departAirport=#{flight.departAirport},arrivalAirport=#{flight.arrivalAirport},departTime=#{flight.departTime},arrivalTime=#{flight.arrivalTime},isOnsell=#{flight.isOnsell} where flightId=#{flight.flightId}")
    int updateFlight(@Param("flight") Flight flight) throws SQLException;

    /**
     * 根据航班id修改在售状态
    * @param flightId
     * @param isOnSell
     * @return
     * @throws SQLException
     */
    @Update("update tb_flight set isOnSell=#{isOnSell} where flightId=#{flightId}")
    int updateIsOnSell(@Param("flightId") String flightId, @Param("isOnSell") Integer isOnSell) throws SQLException;
}
